﻿package game.socket {
	import flash.utils.ByteArray;
	
	public class SocketData {
		
		public static const BYTE:int	= 1;
		public static const SHORT:int	= 2;
		public static const INT:int		= 3;
		public static const STRING:int	= 4;
		public static const FLOAT:int	= 5;
		
		public var type:int;
		public var value:*;
		
		public function SocketData(type:int, value:*):void {
			this.type	= type;
			this.value	= value;
		}
		
		public function get length():int {
			var l:int = 0;
			switch (type) {
				case SocketData.BYTE:		l = 1; break;
				case SocketData.SHORT:		l = 2; break;
				case SocketData.INT:		l = 4; break;
				case SocketData.FLOAT:		l = 4; break;
				case SocketData.STRING:		l = strlen(value); break;
			}
			return l;
		}
		
		public static function strlen (value:String):int {
			var ba:ByteArray = new ByteArray;
			ba.endian = SocketConfig.ENDIAN;
			ba.writeMultiByte(value, SocketConfig.CHARSET);
			return ba.length;
		}
		
		public function toString():String { return type + ': '+String(value); }
		
	}
	
}